package com.wetao.module_pen.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.tstudy.blepenlib.data.BleDevice;
import com.wetao.lib_common.AppConfig;
import com.wetao.lib_common.base.BaseListResponse;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.CheckStudentMacResp;
import com.wetao.lib_common.bean.response.StudentExamGridInfoResp;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.module_pen.contract.PenConnectFragmentCon;

import java.util.HashMap;

import io.reactivex.Observable;
import okhttp3.RequestBody;


/**
 * author: zhouyh
 * created on: 2019-11-15 16:33
 * description:
 */
public class PenConnectFragmentPre extends PenConnectFragmentCon.Presenter {


    private Context mContext;

    public PenConnectFragmentPre(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void checkStudentMac(HashMap<String, String> map, boolean isDialog, boolean cancelable, BleDevice posBle) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().checkStudentMac(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<CheckStudentMacResp>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<CheckStudentMacResp>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<CheckStudentMacResp>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<CheckStudentMacResp> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().checkStudentMacResult(baseResponse, posBle);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().checkStudentMacFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        getView().checkStudentMacFailed();
                    }
                });
    }

    @Override
    public void studentConfirmConnect(HashMap<String, String> map, boolean isDialog, boolean cancelable, BleDevice posBle) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().studentConfirmConnect(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<CheckStudentMacResp>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<CheckStudentMacResp>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<CheckStudentMacResp>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<CheckStudentMacResp> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().studentConfirmConnectResult(baseResponse, posBle);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().studentConfirmConnectFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        getView().studentConfirmConnectFailed();
                    }
                });
    }

    @Override
    public void studentExamGridInfo(String json, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        Observable<BaseListResponse<StudentExamGridInfoResp>> observable =  null;
        //if(AppConfig.BASE_URL.equals(AppConfig.TEST_AHEAD_URL_1) || AppConfig.BASE_URL.equals(AppConfig.PRE_PUBLISH_URL)) {
            observable =  ServerUtils.getCommonApi().pictrueGridInfo(requestBody);
        /*} else {
            observable =  ServerUtils.getCommonApi().studentExamGridInfo(requestBody);
        }*/
        observable.retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<StudentExamGridInfoResp>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<StudentExamGridInfoResp>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<StudentExamGridInfoResp>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<StudentExamGridInfoResp> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().studentExamGridInfoResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().studentExamGridInfoFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        getView().studentExamGridInfoFailed();
                    }
                });
    }

    @Override
    public void studentExamCommitPenData(String json, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().studentExamCommitPenData(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().studentExamCommitPenDataResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().studentExamCommitPenDataFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        getView().studentExamCommitPenDataFailed();
                    }
                });
    }
}
